//app.js
App({
    onLaunch: function() {
        //调用API从本地缓存中获取数据
        var logs = wx.getStorageSync('logs') || []
        logs.unshift(Date.now())
        wx.setStorageSync('logs', logs)
        wx.login({
          success: function (res) {
            // console.log(res);
            if (res.code) {
              //发起网络请求
              wx.request({
                url: 'https://shop.yougou520.cn/api/func/get_openid',
                method:'POST',
                data: {
                  code: res.code
                },
                success:function(o){
                  // console.log(o);
                  wx.getUserInfo({
                    success: function (info) {
                      // console.log(info);
                      wx.request({
                        url: 'https://shop.yougou520.cn/api/func/change_user',
                        method:'POST',
                        data:{
                          openid:o.data.openid,
                          oauth:'mini',
                          nickname: info.userInfo.nickName,
                          head_pic: info.userInfo.avatarUrl,
                          sex: info.userInfo.gender,
                        },
                        success:function(an){
                          console.log(an.data.result)
                          wx.setStorageSync('open_id', o.data.openid);
                          wx.setStorageSync('user_id', an.data.result.user_id);
                          wx.setStorageSync('user', an.data.result);
                        }
                      })
                    }
                  })
                }
              })
            } else {
              console.log('获取用户登录态失败！' + res.errMsg)
            }
          }
      });
    },

    getUserInfo: function(cb) {
        var that = this
        if (this.globalData.userInfo) {
            typeof cb == "function" && cb(this.globalData.userInfo)
        } else {
            //调用登录接口
            wx.getUserInfo({
                withCredentials: false,
                success: function(res) {
                    that.globalData.userInfo = res.userInfo
                    typeof cb == "function" && cb(that.globalData.userInfo)
                }
            })
        }
    },

    globalData: {
        userInfo: null,
        url: 'https://shop.yougou520.cn'
    },
    /**
     * 页面跳转函数
     * data-page -> 跳转页面
     * data-data -> 页面参数
     */
    goToPage: function(e) {
        //   console.log(e.currentTarget.dataset);
        if (e.currentTarget.dataset.data) {
          wx.navigateTo({
                url: '/pages/' + e.currentTarget.dataset.page + '/' + e.currentTarget.dataset.page + '?' + e.currentTarget.dataset.data,
            })
        } else {
          wx.navigateTo({
                url: '/pages/' + e.currentTarget.dataset.page + '/' + e.currentTarget.dataset.page,
            })
        }
    },
    goToTabBar:function(e){
      if (e.currentTarget.dataset.data) {
        wx.switchTab({
          url: '/pages/' + e.currentTarget.dataset.page + '/' + e.currentTarget.dataset.page + '?' + e.currentTarget.dataset.data,
        })
      } else {
        wx.switchTab({
          url: '/pages/' + e.currentTarget.dataset.page + '/' + e.currentTarget.dataset.page,
        })
      }
    },
    /**
     * 获取页面路由
     */
    getUrl: function(e) {
        var url = getCurrentPages();
        var url1 = '/' + url[0].route + '?';
        for (var i in e) {
            url1 += i + '=' + e[i] + '&';
        }
        var value = wx.getStorageSync('pages');
        if (value != '') {
            if (value[value.length - 1] != url1) {
                value.push(url1);
                wx.setStorageSync('pages', value);
            }
        } else {
            wx.setStorageSync('pages', [url1])
        }
        //   console.log(wx.getStorageSync('pages'));
    },
    /**
     * 全局返回上一页函数
     */
    returnPage: function(e) {
        var value = wx.getStorageSync('pages');
        value.pop();
        wx.setStorageSync('pages', value)
        wx.reLaunch({
            url: value[value.length - 1]
        })
    }

})